$(function () {

    //初始就铺满地面
    (function () {
        var num = Math.ceil($('.wrap').width() / 40);
        for (var i = 0; i < (num + 1); i++) {
            $(".wollWrap").append($("<li class='woll'></li>"));
        }
    })();
    //end

    //键盘按下键位，使小人动起来
    function People(people, wollwrap) {
        this.people = people;
        this.wollwrap = wollwrap;
        this.document = $(document);
    }
    //end

    //人动
    People.prototype.superMlaWoll = function () {
        var that = this;
        var flag = true;
        function onkeyDown(ev) {
            if (ev.keyCode == 37) {
                if (parseInt(that.people.css("left")) < 0) {
                    that.people.css("left", "0");
                } else {
                    var peopleNow = parseInt(that.people.css("left"));
                    that.people.css('left', peopleNow -= 4);
                }
            } else if (ev.keyCode == 39) {
                if (parseInt(that.people.css("left")) >= 200) {
                    that.people.css("left", 200);
                } else {
                    var peopleNow = parseInt(that.people.css("left"));
                    that.people.css('left', peopleNow += 4);
                }
            }else if (ev.keyCode == 38) {
                jump(that.people);
                peoTouchObs(that.people,[$(".smallTube")]);
            }
            if (parseInt(that.people.css("left")) == 200) {
                $.addMonster();
                $.removeMonster();
                background.backgroundAdd();
                background.backgroundMove();
                obstacleBackground.obstacleMove();
                peoTouchObs(that.people,[$(".smallTube")]);
                boundary(that.wollwrap);
            }
        }
        this.document.on("keydown",onkeyDown)
        $.moveMonster();
    }
    //end

    //墙体移动
    function boundary(wollWrap) {
        if (parseInt(wollWrap.css("left")) <= -40) {
            $(".wollWrap .woll:first-child").remove()
            wollWrap.css("left", 0);
            wollWrap.append($("<li class='woll'></li>"));
        } else {
            var wollNow = parseInt(wollWrap.css("left"));
            wollWrap.css("left", wollNow -= 4);
        }
    }
    //end

    //跳跃
    function jump(player) {
        player.stop(true).animate({
            bottom: 240
        }, 1300, function () {
            player.stop(true).animate({
                bottom: 80
            }, 1300);
        })
    }
    //end

    //背景（山、云、草）;
    function Background(mountain, cloud, grass) {
        this.mountain = mountain;
        this.cloud = cloud;
        this.grass = grass;
    }

    //背景图移动
    Background.prototype.backgroundMove = function () {
        backgroundAll([this.mountain, this.cloud, this.grass]);
        this.mountain = $(".mountain");
        this.cloud = $(".cloud");
        this.grass = $(".grass");
    }
    //end

    //所有背景图都移动
    function backgroundAll(arr) {
        $.each(arr, function (idx, item) {
            $(item).each(function (idx, item1) {
                var temporary = parseInt($(item1).css("left"));
                $(item1).css({
                    left: temporary -= 4
                });
                if (temporary <= -$(item1).width()) {
                    $(item1).remove();
                }
            })
        })
    }

    //随机添加背景（山、云、草）
    Background.prototype.backgroundAdd = function () {
        var whenAdd = (Math.random() * 10).toFixed(1);
        if (whenAdd > "9.2") {
            randomAdd(this.mountain, this.cloud, this.grass);
        }
    }
    //end

    //随机添加背景（山、云、草）
    function randomAdd(fnMountain,fnCloud,fnGrass) {
        var randomNum = Math.round(Math.random() * 10);
        if (randomNum >= 0 && randomNum < 1) {
            randomEle("mountain");
            fnMountain = $(".cloud");
        } else if (randomNum >= 1 && randomNum < 2) {
            randomEle("cloud");
            fnCloud = $(".cloud");
        } else if (randomNum >= 2 && randomNum <= 3) {
            randomEle("grass");
            fnGrass = $(".grass");
        }
    }
    function randomEle(className){
        $(".wrap").append($("<div class='"+className+"'></div>"));
        var randomAddLength = $("."+className).length;
        console.log(randomAddLength);
        $("."+className+":eq(" + (randomAddLength - 1) + ")").css({
            bottom: 80,
            left: 800
        })
    }
    //end

        //随机生成障碍物
    //障碍物跟随角色移动
    function Obstacle(tube) {
        this.tube = tube;
    }
    Obstacle.prototype.obstacleMove = function () {
        var ObsRandom = Math.random()*10;
        if(ObsRandom>9.9){
            randomEle("smallTube");
            this.tube = $(".smallTube")
        }
        backgroundAll([this.tube]);
    }
    var obstacleBackground = new Obstacle($(".smallTube"));
    //end

    //障碍物碰撞检测
    function peoTouchObs(peo,ObsArr,callback){
        backgroundAll([this.tube])
        $.each(ObsArr,function(idx,item){
            $(item).each(function(idx,item1){
                if(parseInt(peo.css("left"))+peo.width() >= parseInt($(item1).css("left")) && parseInt(peo.css("left")) <= parseInt($(item1).css("left"))+$(item1).width()){
                    stop(item1,4)
                }
                if(parseInt(peo.css("bottom")) >=parseInt($(item1).css("bottom")) + $(item1).height()){
                    stop(item1,-4)
                }

            })
        })
    }
    function stop(item1,algo){
        var wollWrap = parseInt($(".wollWrap").css("left"));
        var obsNow = parseInt($(item1).css("left"));
        var mountainNow = parseInt($(".mountain").css("left"));
        var cloudNow = parseInt($(".cloud").css("left"));
        var grassNow = parseInt($(".grass").css("left"));
        $(".wollWrap").css("left",wollWrap += algo);
        $(item1).css("left",obsNow+= algo);
        $(".mountain").css("left",mountainNow+= algo);
        $(".cloud").css("left",cloudNow+= algo);
        $(".grass").css("left",grassNow+=algo);
    }

    var background = new Background($(".mountain"), $(".cloud"), $(".grass"));

    var mla = new People($(".people"), $(".wollWrap"));
    mla.superMlaWoll();
})